package com.hymall.service.admin;

import com.hymall.bean.MarketPermission;
import com.hymall.bean.MarketPermissionExample;
import com.hymall.bean.MarketPerms;
import com.hymall.bean.MarketPermsExample;
import com.hymall.bean.bo.PermissionsPostBo;
import com.hymall.bean.vo.PermissionsFirstLayerVo;
import com.hymall.bean.vo.PermissionsVo;
import com.hymall.mapper.MarketPermissionMapper;
import com.hymall.mapper.MarketPermsMapper;
import org.apache.commons.collections.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 叶熠彬
 * @description:
 * @date: 2022/7/17 22:46
 */
@Service
public class PermissionsServiceImpl implements PermissionsService {

    @Autowired
    MarketPermissionMapper permissionMapper;
    @Autowired
    MarketPermsMapper permsMapper;

    /**
     * Special ThanksTo: 费总建的表
     * 根据ID查询全部已有权限信息，并返回所有可选权限信息
     *
     * @author: 叶熠彬
     * @date: 2022/7/17 23:00
     * @param: [roleId]
     * @return: com.hymall.bean.common.CommonData
     */
    @Override
    public PermissionsVo queryPermissions(int roleId) {
        PermissionsVo permissionsVo = new PermissionsVo();
        //显示全部权限
        List<PermissionsFirstLayerVo> firstLayerVos = permsMapper.selectAllPerms();
        permissionsVo.setSystemPermissions(firstLayerVos);

        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();

        //根据roleId查permission表，返回全部已有权限
        if (roleId != 1) {
            criteria.andRoleIdEqualTo(roleId);
            criteria.andDeletedEqualTo(false);
        }
        List<String> assignedPermissions = new ArrayList<>();
        List<MarketPermission> marketPermissions = permissionMapper.selectByExample(example);
        for (MarketPermission marketPermission : marketPermissions) {
            String id = marketPermission.getPermission();
            assignedPermissions.add(id);
        }
        permissionsVo.setAssignedPermissions(assignedPermissions);
        return permissionsVo;
    }

    /**
     * 授权操作，更改权限信息
     *
     * @author: 叶熠彬
     * @date: 2022/7/18 9:00
     * @param: [bo]
     * @return: com.hymall.bean.vo.PermissionsVo
     */
    @Override
    public int updatePermissions(PermissionsPostBo bo) {

        List<String> permissions = bo.getPermissions();
        List<String> existedPermissions = permissionMapper.selectPermissionsByRoleId(bo.getRoleId());
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(bo.getRoleId());
        //空的permission，先把该角色的所有deleted全部置为true,更新时间
        MarketPermission permission = new MarketPermission();
        permission.setUpdateTime(new Date());
        permission.setDeleted(true);
        permissionMapper.updateByExampleSelective(permission, example);

        //求已存在的permission 更新状态false
        List<String> intersection = ListUtils.retainAll(permissions, existedPermissions);
        //限制roleID,已存在的permission
        if (intersection.size() != 0) {
            criteria.andPermissionIn(intersection);
            permission.setDeleted(false);
            permissionMapper.updateByExampleSelective(permission, example);
        }

        List<String> newPermissions = ListUtils.subtract(permissions, intersection);
        for (String newPermission : newPermissions) {
            //在permission表新增一条记录
            MarketPermission role = new MarketPermission();
            role.setRoleId(bo.getRoleId());
            role.setUpdateTime(new Date());
            role.setAddTime(new Date());
            role.setDeleted(false);
            role.setPermission(newPermission);
            //插入
            try {
                permissionMapper.insertSelective(role);
            } catch (Exception e) {
                e.printStackTrace();
                return 201;
            }
        }
        return 200;

    }

    /**
     * 根据roleID数组查找权限信息
     * 查找perms的API
     *
     * @author: 叶熠彬
     * @date: 2022/7/18 22:11
     * @param: [ids]
     * @return: java.util.List<java.lang.String>
     */
    @Override
    public List<String> queryPermissionListByRoleIds(Integer[] ids) {
//        List<String> permissions = permissionMapper.selectPermissionsByAdminRoleIds(ids);
        List<String> permissions = permissionMapper.selectAvailablePermissionsByAdminRoleIds(ids);
        if (permissions.size() == 1 && "*".equals(permissions.get(0))) {
            return permissions;
        }
        ArrayList<String> perms = new ArrayList<>();

        //查询perms表 返回API
        for (String perm : permissions) {
            MarketPermsExample example = new MarketPermsExample();
            MarketPermsExample.Criteria criteria = example.createCriteria();
            criteria.andPermissionEqualTo(perm);
            List<MarketPerms> marketPerms = permsMapper.selectByExample(example);
            perms.add(marketPerms.get(0).getApi());
        }

        return perms;
    }

}
